var makeupExam_postChangeFunc = function (name,val,$new) {
    var name = name;
    var val = val;
    var dataParam = {id:$new.data('quizId')};
    dataParam.courseId =$("#courseId").val();
    dataParam[name=="duration"?"limitTime":name] = val;
    if(name=="duration" && val==""){
        $($new).find("textarea").val(duration);
        return;
    }else if(name=="duration"){
        duration=val
    }
    $.ajax({
        url:basePath + "/course/thirdStep/StepQuiz/updateExam",
        data: dataParam,
        type:"post",
        async:true,
        success: function (json) {
            customCheckRowIntegrity($new);
        }
    });
}


function _crud_makeupExamContainer(makeupExamListData) {
    if (!initialQueryResult.makeupExam) {
        $(".makeupExamContainer").html(getErrorDisplayHtml("makeupExam"));
        return ;
    }

    // console.log(makeupExamListData);

    return $(".makeupExamContainer").crudArea({
        init: {
            createComponent: function () {
                var $component = $("<div class='makeup_examComponent'></div>");
                $component.append($("<div class='makeup_examItems'></div>"));
                $component.append($("<div class='addMakeupExamHide'></div>"));
                return $component;
            },
            dataSrc:makeupExamListData,
            onInitOver:function(e) {
                var $this = this;
                // console.log(e.initialResultLength);
                // if (e.initialResultLength == 0){
                if (makeupExamListData.length == 0){
                    $this.fireAddOne();
                    // $this.drawOneProcess(e, {});

                }
            }
        },
        add: {
            btnClass: "addMakeupExamHide",
            onAdd: function (e, $btn,newIdx) {
                var $this = this;

                var dataParam = {"courseId" : $("#courseId").val()};
                dataParam.whether=3;
                $.ajax({
                    // url:basePath+"/course/thirdStep/StepQuiz/saveQuestion",
                    url:basePath+"/course/thirdStep/StepQuiz/creatExamid",
                    data: dataParam,
                    async:true,
                    type:"post",
                    success: function (json) {
                        //console.log(json);
                        if (isRequestSuccess(json)) {

                            var rs = {};
                            rs.id=json.result;
                            var $new = $this.drawOneProcess(e, rs);

                            // json.result.id=json.result.examId;
                            // var $new = $this.drawOneProcess(e, json.result);

                            customCheckRowIntegrity($new);
                        }

                    }
                });

            }, containerClass: ".makeup_examItems"
        },
        draw: {
            create: function (e, data) {
                var $new = $(".hidden.container .makeup_exam").clone();
                if (data) {
                    duration=data.limitTime;
                    $new.find("input.makeupExamInputField[name='title']").val(data.title);
                    $new.find("textArea.makeupExamInputField[name='duration']").val(data.limitTime);
                    $new.find("textArea.makeupExamInputField[name='description']").val(data.description);
                    $new.find(".fullMark").text(data.totalScore);
                    $new.find(".totalNumber").text(data.totalQuestionNumber);
                }
                return $new;
            },
            afterCreated: function (e, $new, data) {
                if (data) {
                    $new.data('quizId', data.id);
                }
            },
            draw: function (e, $component, $new, data) {
                $component.children('.makeup_examItems').append($new);
            },
            afterDrawn: function (e, $component, $new, data) {
                e.scene = "makeup_exam";
                if (data) {
                    e.data = data.questionList;
                }
                _crud_quiz_question($.extend({}, e), $component.find(".quizQuestionContainer"), null, $new);
            },
            afterAppend: function(e, $component, $new, data) {
                $new.delegate(".makeupExamInputField","blur",function () {
                    var name = $(this).attr("name");
                    var val = $(this).val();

                    makeupExam_postChangeFunc(name,val,$new);
                });

                bindUEditor($new.find("textArea.makeupExamInputField[name='description']"),data,$new, 'description',makeupExam_postChangeFunc,zLocale.thirdstep_exam_description,'edui-editor-step3-finalExam-default','edui-editor-step3-finalExam-action');

            }
        },
        slide: {
            slideClass: ".makeup_examTitle",
            ignoreSlide: ".ignoreSlide",
            addToggleHeaderCls: "ellipsis-blur   exam-title-item-blur",
            toggleHeaderItem: ".exam-title-item",
            toggleContent: ".makeup_examContent",
            toggleMark:".exam-btn-toggle"
        },
        title_state:{
            inputStateCls:"makeupExamTitleInputState",
            inputLimitCls:"makeupExamTitleLimit",
            inputLimit:120,
            displayStateCls:"makeupExamTitleDisplayState",
            hiddenCls:"hidden",
            editCls:"editMakeupExamTitle",
        },
        integrity:{
            checkRow:function ($row) {
                var $plgThis = this;

                // console.log($row.find(".quizQuestionContainer").crudArea("getItems"));
                var val = $row.find(".quizQuestionContainer").crudArea("getItems").length>0;
                var ret = val;

                var titleNotNull = !isEmpty($row.find("input.makeupExamInputField[name='title']").val());
                // console.log(titleNotNull);
                var durationNotNull = !isEmpty($row.find("textArea.makeupExamInputField[name='duration']").val());
                // console.log(durationNotNull);
                ret = ret & titleNotNull & durationNotNull;
                return ret ;
            },
            whenIntegrityChanged:function ($row,oldIntegrity,newIntegrity) {
                if(newIntegrity){
                    $row.find(".makeup_examIntegrity").removeClass("hintico").addClass("rightico");
                } else {
                    $row.find(".makeup_examIntegrity").removeClass("rightico").addClass("hintico");
                }

                var shouldPost = false;
                //若不为初始化时
                if(oldIntegrity != undefined){
                    // console.log('should post isPass'+newIntegrity);

                    shouldPost = true;
                }

                if(shouldPost){
                    //清除课程缓存
                    cleanTreeRedis($("#courseId").val());
                }

            }
        }

    });


}